package com.sevenwonders;

import java.util.List;

import com.sevenwonders.core.City;
import com.sevenwonders.core.Game;
import com.sevenwonders.core.Player;
import com.sevenwonders.util.Helper;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GameActivity extends Activity {

    private static int lockOrientation = -1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            LayoutInflater inflater = getLayoutInflater();
            RelativeLayout parent = (RelativeLayout) inflater.inflate(R.layout.layout_game, null);
            setContentView(parent);
            List<Player> players = Game.myGame.getPlayers();
            City city = players.get(0)
                .getCity();

            UI.setupCity(parent.findViewById(R.id.layout_player_city), city, city.getSide(), inflater);
            ViewGroup container = null;

            for (int i = 1; i < players.size(); i++) {
                Player opponent = players.get(i);
                City opponentCity = opponent.getCity();
                RelativeLayout opponentLayout = (RelativeLayout) inflater.inflate(R.layout.view_opponent, parent, false);
                opponentLayout.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
                        // TODO:  show opponent city and cards in play
                        
                    }
                });

                TextView cityName = (TextView) opponentLayout.findViewById(R.id.view_opponent_city_name);
                cityName.setText(opponentCity.getName() + " (" + opponentCity.getSide()
                    .getLetter() + ")");

                switch (opponentCity.getStartingBonusId()) {
                case R.drawable.resource_clay:
                    TextView clayView = (TextView) opponentLayout.findViewById(R.id.view_opponent_resource_clay);
                    clayView.setText("1");
                    break;
                case R.drawable.resource_ore:
                    TextView oreView = (TextView) opponentLayout.findViewById(R.id.view_opponent_resource_ore);
                    oreView.setText("1");
                    break;
                case R.drawable.resource_wood:
                    TextView woodView = (TextView) opponentLayout.findViewById(R.id.view_opponent_resource_wood);
                    woodView.setText("1");
                    break;
                case R.drawable.resource_stone:
                    TextView stoneView = (TextView) opponentLayout.findViewById(R.id.view_opponent_resource_stone);
                    stoneView.setText("1");
                    break;
                case R.drawable.resource_glass:
                    TextView glassView = (TextView) opponentLayout.findViewById(R.id.view_opponent_resource_glass);
                    glassView.setText("1");
                    break;
                case R.drawable.resource_cloth:
                    TextView clothView = (TextView) opponentLayout.findViewById(R.id.view_opponent_resource_cloth);
                    clothView.setText("1");
                    break;
                case R.drawable.resource_papyrus:
                    TextView papyrusView = (TextView) opponentLayout.findViewById(R.id.view_opponent_resource_papyrus);
                    papyrusView.setText("1");
                    break;
                }

                if (i == 1) {
                    container = (ViewGroup) parent.findViewById(R.id.layout_neighbor_left);
                } else if (i == players.size() - 1) {
                    container = (ViewGroup) parent.findViewById(R.id.layout_neighbor_right);
                } else {
                    container = (ViewGroup) parent.findViewById(R.id.layout_opponents);
                }

                container.addView(opponentLayout);
            }
        } catch (Throwable t) {
            String stackTrace = Log.getStackTraceString(t);
            Log.e("GameActivity", stackTrace, t);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        lockOrientation = Helper.updateOrientation(this, lockOrientation);
    }
}
